package org.hank.wanandroid.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.fragment_square.*
import org.hank.commom.base.BaseFragment
import org.hank.wanandroid.R
import org.hank.wanandroid.adapter.SquareFragmentAdapter

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/5/27 16:18
 *描述:
 */
class SquareFragment : BaseFragment(R.layout.fragment_square) {
    lateinit var tablayoutMediator: TabLayoutMediator
    lateinit var squareAdapter: SquareFragmentAdapter<SquareFragment>

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        squareAdapter = SquareFragmentAdapter(this)
        viewpager2_square.post {
            tablayoutMediator = TabLayoutMediator(
                tablayout_tab_square,
                viewpager2_square
            ) { tab, position ->
                when (position) {
                    0 -> {
                        tab.text = "体系"
                    }
                    1 -> {
                        tab.text = "导航"
                    }
                }
            }
            tablayoutMediator.attach()
        }
        tablayout_tab_square.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                tab?.apply {
                    viewpager2_square.setCurrentItem(position, true)
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        viewpager2_square.apply {
            adapter = squareAdapter
            offscreenPageLimit = 2
            registerOnPageChangeCallback(viewPager2CallBack)
            isSaveEnabled = false
        }
    }

    var viewPager2CallBack = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            tablayout_tab_square.setScrollPosition(position, positionOffset, true, true)
        }

        override fun onPageSelected(position: Int) {
            tablayout_tab_square.selectTab(
                tablayout_tab_square.getTabAt(
                    position
                )
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        tablayoutMediator.detach()
        viewpager2_square.unregisterOnPageChangeCallback(viewPager2CallBack)
    }
}